<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/26 0026
 * Time: 下午 5:17
 */

namespace Api\Controller;

use Think\Controller;
use Think\Model;

class DishesOrderController extends WeChatController
{
    /**
     * 获取就餐人数
     */
    public function getPerson()
    {

//        $user_id = $this->get_customer_id();
//        if ($user_id) {
        $id = I('post.id');
        $list = M('DishesPeople')->where("table_id='$id'")->field(array('num', 'model'))->select();
//            $this->ajaxReturn(M()->_sql());
        if ($list) {
            foreach ($list as $key => $v) {
                switch ($v['model']) {
                    case 1:
                        $list[$key]['formart_model'] = '小桌';
                        break;
                    case 2:
                        $list[$key]['formart_model'] = '中桌';
                        break;
                    case 3:
                        $list[$key]['formart_model'] = '大桌';
                        break;
                    default:
                        $list[$key]['formart_model'] = '小桌';
                }
            }
            $this->ajaxReturn(array('state' => 1, 'data' => $list), 'json');
        } else {
            $this->ajaxReturn(array('state' => -1), 'json');
        }
//        } else {
//            $this->ajaxReturn(array('state' => 0), 'json');
//        }
    }


    /**
     * 点餐
     */
    public function chooseDishes()
    {
//        $this->ajaxReturn(1);
////        ----------------前段传key=>value---------------
        $data['store_id'] = I('post.store_id');
        //桌号ID
        $data['desk_number'] = I('post.desk_number');
        $data['remark'] = I('post.remark');
        //就餐人数
        $data['people_number'] = I('post.people_number');
        $data['time'] = time();
        $data['order_no'] = ordersn();
        $data['order_state'] = 1;
        $data['total_price']=I('post.totle_price');
        $data['order_price']=I('post.order_price');
        $data['discount_price']=I('post.discount_price');
        $data['customer_id'] = $this->get_customer_id();

        $result = M('dishes_order')->add($data);

        //------------------------------------------------
        $list = I('request.list');
        $countlist = count($list);

        for ($j = 0; $j < $countlist; $j++) {
            $countdata = count($list[$j]['data']);
            for ($i = 0; $i <$countdata; $i++) {
                $data_detail['order_no'] =$data['order_no'];
//                $data_detail['order_id'] =$result;
                $data_detail['dish_id'] = $list[$j]['data'][$i]['good_id'];
                $data_detail['count'] = $list[$j]['data'][$i]['count'];
                $data_detail['price'] = $list[$j]['data'][$i]['good_price'];
                $data_detail['amount'] = $data_detail['price'] * $data_detail['count'];
                $data_detail['create_time'] = time();
                $data_detail['practice'] = $list[$j]['data'][$i]['practice'];
                $data_detail['num'] = $list[$j]['data'][$i]['num'];
                $result_detail = M("order_detail")->add($data_detail);
            }
        }

        if ($result && $result_detail) {
            $this->ajaxreturn(array('state' => 1,'data'=>$result) , 'json');
        } else {
            $this->ajaxreturn(array('state' => -1) , 'json');
        }
    }

    public function orderList(){
        $customer_id = I('get.customer_id');
        $where['customer_id'] = $customer_id;

        $where['valid_flag'] = 1;
        if ($customer_id) {
            //获取商品列表
            $dish_order = M('dishes_order')->where($where)->order("time desc")->select();

            //根据订单id查询订单详情
            if($dish_order) {
                $order_array = array();
                foreach ($dish_order as $key => $v) {
                    switch ($v['order_state']) {
                        case 1:
                            $dish_order[$key]['fromart_state'] = '待付款';
                            break;
                        case 2:
                            $dish_order[$key]['fromart_state'] = '已付款';
                            break;
                    }
                    $order_array[] = $v['order_no'];
                }
                $order_array = implode(',', $order_array);
                $order['order_no'] = array('in', $order_array);
                //获取相同订单里面的order_no
                $dish_detail =  M('order_detail')->alias('o')->join("join meal_dishes as d on d.id = o.dish_id")->field('o.order_no,o.dish_id,o.price,o.count,o.amount,o.practice,d.id as dishId,d.category_id,d.name')->where($order)->order("o.create_time desc")->select();
                $dishDetailArray = array();
                foreach ($dish_order as &$v){
                    foreach ($dish_detail as $val){
                        if($v['order_no'] == $val['order_no']){
                            $v['list'][] = $val;
                        }
                    }
                }
            }
            if ($dish_order) {
                //查询成功
                $this->ajaxreturn(array('state' => 1, 'data' => $dish_order), 'json');
            } else {
                //查询失败
                $this->ajaxreturn(array('state' => -1), 'json');
            }
        } else {
            //未登录
            $this->ajaxreturn(array('state' => 0), 'json');
        }

    }

    /**
     * 立即下单页面获取订单号
     */
    public function getOrderno(){
        $order_id=I('request.order_id');
        $result=M('dishes_order')->where("id='$order_id'")->find();

        if($result){
            $order_no=$result['order_no'];
            $result['detail']=M('order_detail')->where("order_no='$order_no'")->select();

            $this->ajaxReturn(array('state'=>1,'data'=>$result),'json');
        }else{
            $this->ajaxReturn(array('state'=>0),'json');
        }
    }
}